﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class UnitRulesRepository : BaseRepository<UnitOfMeasurementRules>, IUnitRulesRepository
    {
        public UnitRulesRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 添加计量单位换算规则
        /// </summary>
        /// <param name="unitRules"></param>
        /// <returns></returns>
        public bool AddUnitRules(UnitOfMeasurementRules unitRules)
        {
            if (unitRules == null)
            {
                return false;
            }
            Context.UnitOfMeasurementRules.Add(unitRules);

            return Context.SaveChanges() > 0;
        }

        /// <summary>
        /// 删除计量单位换算规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelUnitRules(int id)
        {
            if (id == null)
            {
                return false;
            }
            var list = Context.UnitOfMeasurementRules.Find(id);
            if (list == null)
            {
                return false;
            }
            Context.UnitOfMeasurementRules.Remove(list);
            return Context.SaveChanges() > 0;
        }


        /// <summary>
        /// 查询计量单位换算规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UnitOfMeasurementRules GetUnitRules(int id)
        {
            return Context.UnitOfMeasurementRules.Find(id);
        }

        /// <summary>
        /// 查询计量单位换算规则
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<UnitOfMeasurementRules> GetUnitRulesList(int pageIndex, int pageSize)
        {
            var list = Context.UnitOfMeasurementRules.ToList();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return list;
        }

        /// <summary>
        /// 修改计量单位换算规则
        /// </summary>
        /// <param name="unitRules"></param>
        /// <returns></returns>
        public bool UpdUnitRules(UnitOfMeasurementRules unitRules)
        {
            if (unitRules == null)
            {
                return false;
            }

            var existingRules = Context.UnitOfMeasurementRules.FirstOrDefault(x => x.id == unitRules.id);
            if (existingRules == null)
            {
                return false;
            }

            existingRules.frontUnitId = unitRules.frontUnitId;
            existingRules.frontUnitName = unitRules.frontUnitName;
            existingRules.frontUnitCode = unitRules.frontUnitCode;
            existingRules.frontValue = unitRules.frontValue;
            existingRules.alertUnitId = unitRules.alertUnitId;
            existingRules.alertUnitName = unitRules.alertUnitName;
            existingRules.alertUnitCode = unitRules.alertUnitCode;
            existingRules.alertValue = unitRules.alertValue;
            Context.UnitOfMeasurementRules.Update(existingRules);
            return Context.SaveChanges() > 0;
        }

    }
}
